{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Finding Natural Breaks in Data with the Fisher-Jenks Algorithm\n",
    "\n",
    "Notebook accompanying article on [PB Python](https://pbpython.com/natural-breaks.html)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "import jenkspy"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Create an example dataframe"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "sales = {\n",
    "    'account': [\n",
    "        'Jones Inc', 'Alpha Co', 'Blue Inc', 'Super Star Inc', 'Wamo',\n",
    "        'Next Gen', 'Giga Co', 'IniTech', 'Beta LLC'\n",
    "    ],\n",
    "    'Total': [1500, 2100, 50, 20, 75, 1100, 950, 1300, 1400]\n",
    "}\n",
    "df = pd.DataFrame(sales)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>account</th>\n",
       "      <th>Total</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>Super Star Inc</td>\n",
       "      <td>20</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>Blue Inc</td>\n",
       "      <td>50</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>Wamo</td>\n",
       "      <td>75</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>Giga Co</td>\n",
       "      <td>950</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>Next Gen</td>\n",
       "      <td>1100</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>IniTech</td>\n",
       "      <td>1300</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>Beta LLC</td>\n",
       "      <td>1400</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>Jones Inc</td>\n",
       "      <td>1500</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>Alpha Co</td>\n",
       "      <td>2100</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "          account  Total\n",
       "3  Super Star Inc     20\n",
       "2        Blue Inc     50\n",
       "4            Wamo     75\n",
       "6         Giga Co    950\n",
       "5        Next Gen   1100\n",
       "7         IniTech   1300\n",
       "8        Beta LLC   1400\n",
       "0       Jones Inc   1500\n",
       "1        Alpha Co   2100"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.sort_values(by='Total')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Try cutting the data using qcut"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "df['quantile'] = pd.qcut(df['Total'], q=2, labels=['bucket_1', 'bucket_2'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>account</th>\n",
       "      <th>Total</th>\n",
       "      <th>quantile</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>Super Star Inc</td>\n",
       "      <td>20</td>\n",
       "      <td>bucket_1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>Blue Inc</td>\n",
       "      <td>50</td>\n",
       "      <td>bucket_1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>Wamo</td>\n",
       "      <td>75</td>\n",
       "      <td>bucket_1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>Giga Co</td>\n",
       "      <td>950</td>\n",
       "      <td>bucket_1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>Next Gen</td>\n",
       "      <td>1100</td>\n",
       "      <td>bucket_1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>IniTech</td>\n",
       "      <td>1300</td>\n",
       "      <td>bucket_2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>Beta LLC</td>\n",
       "      <td>1400</td>\n",
       "      <td>bucket_2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>Jones Inc</td>\n",
       "      <td>1500</td>\n",
       "      <td>bucket_2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>Alpha Co</td>\n",
       "      <td>2100</td>\n",
       "      <td>bucket_2</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "          account  Total  quantile\n",
       "3  Super Star Inc     20  bucket_1\n",
       "2        Blue Inc     50  bucket_1\n",
       "4            Wamo     75  bucket_1\n",
       "6         Giga Co    950  bucket_1\n",
       "5        Next Gen   1100  bucket_1\n",
       "7         IniTech   1300  bucket_2\n",
       "8        Beta LLC   1400  bucket_2\n",
       "0       Jones Inc   1500  bucket_2\n",
       "1        Alpha Co   2100  bucket_2"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.sort_values(by='Total')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Compare with using cut"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "df['cut_bins'] = pd.cut(df['Total'],\n",
    "                         bins=2,\n",
    "                         labels=['bucket_1', 'bucket_2'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>account</th>\n",
       "      <th>Total</th>\n",
       "      <th>quantile</th>\n",
       "      <th>cut_bins</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>Super Star Inc</td>\n",
       "      <td>20</td>\n",
       "      <td>bucket_1</td>\n",
       "      <td>bucket_1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>Blue Inc</td>\n",
       "      <td>50</td>\n",
       "      <td>bucket_1</td>\n",
       "      <td>bucket_1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>Wamo</td>\n",
       "      <td>75</td>\n",
       "      <td>bucket_1</td>\n",
       "      <td>bucket_1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>Giga Co</td>\n",
       "      <td>950</td>\n",
       "      <td>bucket_1</td>\n",
       "      <td>bucket_1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>Next Gen</td>\n",
       "      <td>1100</td>\n",
       "      <td>bucket_1</td>\n",
       "      <td>bucket_2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>IniTech</td>\n",
       "      <td>1300</td>\n",
       "      <td>bucket_2</td>\n",
       "      <td>bucket_2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>Beta LLC</td>\n",
       "      <td>1400</td>\n",
       "      <td>bucket_2</td>\n",
       "      <td>bucket_2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>Jones Inc</td>\n",
       "      <td>1500</td>\n",
       "      <td>bucket_2</td>\n",
       "      <td>bucket_2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>Alpha Co</td>\n",
       "      <td>2100</td>\n",
       "      <td>bucket_2</td>\n",
       "      <td>bucket_2</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "          account  Total  quantile  cut_bins\n",
       "3  Super Star Inc     20  bucket_1  bucket_1\n",
       "2        Blue Inc     50  bucket_1  bucket_1\n",
       "4            Wamo     75  bucket_1  bucket_1\n",
       "6         Giga Co    950  bucket_1  bucket_1\n",
       "5        Next Gen   1100  bucket_1  bucket_2\n",
       "7         IniTech   1300  bucket_2  bucket_2\n",
       "8        Beta LLC   1400  bucket_2  bucket_2\n",
       "0       Jones Inc   1500  bucket_2  bucket_2\n",
       "1        Alpha Co   2100  bucket_2  bucket_2"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.sort_values(by='Total')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Show how jenkspy works"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[20.0, 75.0, 2100.0]\n"
     ]
    }
   ],
   "source": [
    "breaks = jenkspy.jenks_breaks(df['Total'], nb_class=2)\n",
    "print(breaks)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>account</th>\n",
       "      <th>Total</th>\n",
       "      <th>quantile</th>\n",
       "      <th>cut_bins</th>\n",
       "      <th>cut_jenks</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>Super Star Inc</td>\n",
       "      <td>20</td>\n",
       "      <td>bucket_1</td>\n",
       "      <td>bucket_1</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>Blue Inc</td>\n",
       "      <td>50</td>\n",
       "      <td>bucket_1</td>\n",
       "      <td>bucket_1</td>\n",
       "      <td>bucket_1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>Wamo</td>\n",
       "      <td>75</td>\n",
       "      <td>bucket_1</td>\n",
       "      <td>bucket_1</td>\n",
       "      <td>bucket_1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>Giga Co</td>\n",
       "      <td>950</td>\n",
       "      <td>bucket_1</td>\n",
       "      <td>bucket_1</td>\n",
       "      <td>bucket_2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>Next Gen</td>\n",
       "      <td>1100</td>\n",
       "      <td>bucket_1</td>\n",
       "      <td>bucket_2</td>\n",
       "      <td>bucket_2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>IniTech</td>\n",
       "      <td>1300</td>\n",
       "      <td>bucket_2</td>\n",
       "      <td>bucket_2</td>\n",
       "      <td>bucket_2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>Beta LLC</td>\n",
       "      <td>1400</td>\n",
       "      <td>bucket_2</td>\n",
       "      <td>bucket_2</td>\n",
       "      <td>bucket_2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>Jones Inc</td>\n",
       "      <td>1500</td>\n",
       "      <td>bucket_2</td>\n",
       "      <td>bucket_2</td>\n",
       "      <td>bucket_2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>Alpha Co</td>\n",
       "      <td>2100</td>\n",
       "      <td>bucket_2</td>\n",
       "      <td>bucket_2</td>\n",
       "      <td>bucket_2</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "          account  Total  quantile  cut_bins cut_jenks\n",
       "3  Super Star Inc     20  bucket_1  bucket_1       NaN\n",
       "2        Blue Inc     50  bucket_1  bucket_1  bucket_1\n",
       "4            Wamo     75  bucket_1  bucket_1  bucket_1\n",
       "6         Giga Co    950  bucket_1  bucket_1  bucket_2\n",
       "5        Next Gen   1100  bucket_1  bucket_2  bucket_2\n",
       "7         IniTech   1300  bucket_2  bucket_2  bucket_2\n",
       "8        Beta LLC   1400  bucket_2  bucket_2  bucket_2\n",
       "0       Jones Inc   1500  bucket_2  bucket_2  bucket_2\n",
       "1        Alpha Co   2100  bucket_2  bucket_2  bucket_2"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df['cut_jenks'] = pd.cut(df['Total'],\n",
    "                         bins=breaks,\n",
    "                         labels=['bucket_1', 'bucket_2'])\n",
    "df.sort_values(by='Total')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Fix the NaN by using include_lowest"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>account</th>\n",
       "      <th>Total</th>\n",
       "      <th>quantile</th>\n",
       "      <th>cut_bins</th>\n",
       "      <th>cut_jenks</th>\n",
       "      <th>cut_jenksv2</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>Super Star Inc</td>\n",
       "      <td>20</td>\n",
       "      <td>bucket_1</td>\n",
       "      <td>bucket_1</td>\n",
       "      <td>NaN</td>\n",
       "      <td>bucket_1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>Blue Inc</td>\n",
       "      <td>50</td>\n",
       "      <td>bucket_1</td>\n",
       "      <td>bucket_1</td>\n",
       "      <td>bucket_1</td>\n",
       "      <td>bucket_1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>Wamo</td>\n",
       "      <td>75</td>\n",
       "      <td>bucket_1</td>\n",
       "      <td>bucket_1</td>\n",
       "      <td>bucket_1</td>\n",
       "      <td>bucket_1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>Giga Co</td>\n",
       "      <td>950</td>\n",
       "      <td>bucket_1</td>\n",
       "      <td>bucket_1</td>\n",
       "      <td>bucket_2</td>\n",
       "      <td>bucket_2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>Next Gen</td>\n",
       "      <td>1100</td>\n",
       "      <td>bucket_1</td>\n",
       "      <td>bucket_2</td>\n",
       "      <td>bucket_2</td>\n",
       "      <td>bucket_2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>IniTech</td>\n",
       "      <td>1300</td>\n",
       "      <td>bucket_2</td>\n",
       "      <td>bucket_2</td>\n",
       "      <td>bucket_2</td>\n",
       "      <td>bucket_2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>Beta LLC</td>\n",
       "      <td>1400</td>\n",
       "      <td>bucket_2</td>\n",
       "      <td>bucket_2</td>\n",
       "      <td>bucket_2</td>\n",
       "      <td>bucket_2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>Jones Inc</td>\n",
       "      <td>1500</td>\n",
       "      <td>bucket_2</td>\n",
       "      <td>bucket_2</td>\n",
       "      <td>bucket_2</td>\n",
       "      <td>bucket_2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>Alpha Co</td>\n",
       "      <td>2100</td>\n",
       "      <td>bucket_2</td>\n",
       "      <td>bucket_2</td>\n",
       "      <td>bucket_2</td>\n",
       "      <td>bucket_2</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "          account  Total  quantile  cut_bins cut_jenks cut_jenksv2\n",
       "3  Super Star Inc     20  bucket_1  bucket_1       NaN    bucket_1\n",
       "2        Blue Inc     50  bucket_1  bucket_1  bucket_1    bucket_1\n",
       "4            Wamo     75  bucket_1  bucket_1  bucket_1    bucket_1\n",
       "6         Giga Co    950  bucket_1  bucket_1  bucket_2    bucket_2\n",
       "5        Next Gen   1100  bucket_1  bucket_2  bucket_2    bucket_2\n",
       "7         IniTech   1300  bucket_2  bucket_2  bucket_2    bucket_2\n",
       "8        Beta LLC   1400  bucket_2  bucket_2  bucket_2    bucket_2\n",
       "0       Jones Inc   1500  bucket_2  bucket_2  bucket_2    bucket_2\n",
       "1        Alpha Co   2100  bucket_2  bucket_2  bucket_2    bucket_2"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df['cut_jenksv2'] = pd.cut(df['Total'],\n",
    "                         bins=breaks,\n",
    "                         labels=['bucket_1', 'bucket_2'],\n",
    "                         include_lowest=True)\n",
    "df.sort_values(by='Total')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Try some other examples"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>account</th>\n",
       "      <th>Total</th>\n",
       "      <th>quantile</th>\n",
       "      <th>cut_bins</th>\n",
       "      <th>cut_jenks</th>\n",
       "      <th>cut_jenksv2</th>\n",
       "      <th>quantilev2</th>\n",
       "      <th>cut_jenksv3</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>Super Star Inc</td>\n",
       "      <td>20</td>\n",
       "      <td>bucket_1</td>\n",
       "      <td>bucket_1</td>\n",
       "      <td>NaN</td>\n",
       "      <td>bucket_1</td>\n",
       "      <td>bucket_1</td>\n",
       "      <td>bucket_1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>Blue Inc</td>\n",
       "      <td>50</td>\n",
       "      <td>bucket_1</td>\n",
       "      <td>bucket_1</td>\n",
       "      <td>bucket_1</td>\n",
       "      <td>bucket_1</td>\n",
       "      <td>bucket_1</td>\n",
       "      <td>bucket_1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>Wamo</td>\n",
       "      <td>75</td>\n",
       "      <td>bucket_1</td>\n",
       "      <td>bucket_1</td>\n",
       "      <td>bucket_1</td>\n",
       "      <td>bucket_1</td>\n",
       "      <td>bucket_1</td>\n",
       "      <td>bucket_1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>Giga Co</td>\n",
       "      <td>950</td>\n",
       "      <td>bucket_1</td>\n",
       "      <td>bucket_1</td>\n",
       "      <td>bucket_2</td>\n",
       "      <td>bucket_2</td>\n",
       "      <td>bucket_2</td>\n",
       "      <td>bucket_2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>Next Gen</td>\n",
       "      <td>1100</td>\n",
       "      <td>bucket_1</td>\n",
       "      <td>bucket_2</td>\n",
       "      <td>bucket_2</td>\n",
       "      <td>bucket_2</td>\n",
       "      <td>bucket_2</td>\n",
       "      <td>bucket_2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>IniTech</td>\n",
       "      <td>1300</td>\n",
       "      <td>bucket_2</td>\n",
       "      <td>bucket_2</td>\n",
       "      <td>bucket_2</td>\n",
       "      <td>bucket_2</td>\n",
       "      <td>bucket_3</td>\n",
       "      <td>bucket_3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>Beta LLC</td>\n",
       "      <td>1400</td>\n",
       "      <td>bucket_2</td>\n",
       "      <td>bucket_2</td>\n",
       "      <td>bucket_2</td>\n",
       "      <td>bucket_2</td>\n",
       "      <td>bucket_3</td>\n",
       "      <td>bucket_3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>Jones Inc</td>\n",
       "      <td>1500</td>\n",
       "      <td>bucket_2</td>\n",
       "      <td>bucket_2</td>\n",
       "      <td>bucket_2</td>\n",
       "      <td>bucket_2</td>\n",
       "      <td>bucket_4</td>\n",
       "      <td>bucket_3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>Alpha Co</td>\n",
       "      <td>2100</td>\n",
       "      <td>bucket_2</td>\n",
       "      <td>bucket_2</td>\n",
       "      <td>bucket_2</td>\n",
       "      <td>bucket_2</td>\n",
       "      <td>bucket_4</td>\n",
       "      <td>bucket_4</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "          account  Total  quantile  cut_bins cut_jenks cut_jenksv2 quantilev2  \\\n",
       "3  Super Star Inc     20  bucket_1  bucket_1       NaN    bucket_1   bucket_1   \n",
       "2        Blue Inc     50  bucket_1  bucket_1  bucket_1    bucket_1   bucket_1   \n",
       "4            Wamo     75  bucket_1  bucket_1  bucket_1    bucket_1   bucket_1   \n",
       "6         Giga Co    950  bucket_1  bucket_1  bucket_2    bucket_2   bucket_2   \n",
       "5        Next Gen   1100  bucket_1  bucket_2  bucket_2    bucket_2   bucket_2   \n",
       "7         IniTech   1300  bucket_2  bucket_2  bucket_2    bucket_2   bucket_3   \n",
       "8        Beta LLC   1400  bucket_2  bucket_2  bucket_2    bucket_2   bucket_3   \n",
       "0       Jones Inc   1500  bucket_2  bucket_2  bucket_2    bucket_2   bucket_4   \n",
       "1        Alpha Co   2100  bucket_2  bucket_2  bucket_2    bucket_2   bucket_4   \n",
       "\n",
       "  cut_jenksv3  \n",
       "3    bucket_1  \n",
       "2    bucket_1  \n",
       "4    bucket_1  \n",
       "6    bucket_2  \n",
       "5    bucket_2  \n",
       "7    bucket_3  \n",
       "8    bucket_3  \n",
       "0    bucket_3  \n",
       "1    bucket_4  "
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df['quantilev2'] = pd.qcut(\n",
    "    df['Total'], q=4, labels=['bucket_1', 'bucket_2', 'bucket_3', 'bucket_4'])\n",
    "\n",
    "df['cut_jenksv3'] = pd.cut(\n",
    "    df['Total'],\n",
    "    bins=jenkspy.jenks_breaks(df['Total'], nb_class=4),\n",
    "    labels=['bucket_1', 'bucket_2', 'bucket_3', 'bucket_4'],\n",
    "    include_lowest=True)\n",
    "\n",
    "\n",
    "df.sort_values(by='Total')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
